﻿SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[bhp_ApartmentPriceProfileGet]
	@ArrivalDate	DATETIME,
	@DepartureDate	DATETIME,
	@ApartmentId	INT,
	@PeopleCount	INT
AS
BEGIN

	DECLARE 
		@UAD	NUMERIC(18,0),
		@UDD	NUMERIC(18,0)
	
     SET @UAD			= DATEDIFF(d,'01-01-2001',@ArrivalDate)+73053
     SET @UDD			= DATEDIFF(d,'01-01-2001',@DepartureDate)+73053
	
		PRINT @UAD
	
	DECLARE
		@Result TABLE(dzien	INT, wartosc NUMERIC(18,2), nazwa NVARCHAR(100), rabatlf NUMERIC(18,2), rabatdl NUMERIC(18,2), wartosc_rabat NUMERIC(18,2))
	
	INSERT INTO @Result
	EXEC	[dbo].[DST_PRICE_GET]
			@id_apartamentu = @ApartmentId,
			@od_daty = @UAD,
			@do_daty = @UDD,
			@liczba_osob = @PeopleCount

	
	SELECT TOP 1 
		dzien as DayCount,
		--ISNULL(wartosc, 0) as PriceWithoutRabat, 
		ISNULL(wartosc, 0) as Price, 
		--ISNULL(wartosc_rabat, 0) as Price, 
		rabatlf + rabatdl as Rabat 
	FROM 
		@Result

END
GO
CREATE PROCEDURE [bhp_DiscountsLastGet]
AS
	SELECT 
		a.typ_apartamentu as ApartmentTypeId,
		cl.do_ilu_last as DayCount,
		cl.rabat as Rabat 
	FROM 
		APARTAMENTY a
		INNER JOIN Z_CENA__LAST cl ON a.cena = cl.cena
	WHERE
		a.del = 0
		AND cl.del = 0
GO
CREATE PROCEDURE [bhp_DiscountsFirstGet]
AS
	SELECT 
		a.typ_apartamentu as ApartmentTypeId,
		cf.od_ilu_first as DayCount,
		cf.rabat as Rabat 
	FROM 
		APARTAMENTY a
		INNER JOIN Z_CENA__FIRST cf ON a.cena = cf.cena
	WHERE
		a.del = 0
		AND cf.del = 0
GO
CREATE PROCEDURE [bhp_DiscountsLengthOfStayGet]
AS
	SELECT 
		a.typ_apartamentu as ApartmentTypeId,
		cdp.ilosc_dni as DayCount,
		cdp.rabat as Rabat 
	FROM 
		APARTAMENTY a
		INNER JOIN Z_CENA__DLUGOSC_POBYTU cdp ON a.cena = cdp.cena
	WHERE
		a.del = 0
		AND cdp.del = 0